package Unicart.applogic3;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import Unicart.applogic.UserHelper;
import Unicart.model.User;
import Unicart.pages.UserProfilePage;

public class UserHelper3 extends DriverBasedHelper implements UserHelper {
	
	public UserHelper3(ApplicationManager3 manager) {
	    super(manager.getWebDriver());
	}
	
	@Override
	  public void loginAs(User user) {
	    if (driver.findElements(By.linkText("Войти")).size() > 0) {
		pages.mainPage
			.ensurePageLoaded()
	    	.clickEnterLink();	
		}
		pages.loginPage
	      	.ensurePageLoaded()
	      	.setUsernameField(user.getLogin())
	      	.setPasswordField(user.getPassword())
	      	.clickSubmitButton(); 
	}
	
	@Override
	  public void logout() {
	    pages.internalPage
	      .ensurePageLoaded()
	      .clickLogoutLink();
	}

	@Override
	public boolean isLoggedIn() {
		 return pages.internalPage.waitPageLoaded();
	}
	
	@Override
	public boolean isLoggedInAs(User user) {
		return isLoggedIn()
	        && getLoggedUser().getLogin().equals(user.getLogin());
	}

	private User getLoggedUser() {
		return new User()
		     .setLogin("slnsk");
	}
	
	 /*private String getLoggedUser() {
	    UserProfilePage userProfile;
		userProfile = pages.internalPage
	      .clickUserProfilePage()
	      .ensurePageLoaded();
	    return new User()
	      .getLogin();
	}*/
	
	@Override
	public boolean isNotLoggedIn() {
		return pages.loginPage.waitPageLoaded();
	}

	@Override
	public void createUser(User user) {
		pages.mainPage.clickRegistrationLink();
		pages.registrationPage.ensurePageLoaded()
			.setUsernameRegField(user.getName())
			.setUsersurnameRegField(user.getSurname())
			.setEmailRegField(user.getEmail())
			.setLoginRegField(user.getLogin())
			.setRegPassword(user.getPassword())
			.setRegPassword2(user.getPassword())
			.clickRegAgreementCheckbox()
			.clickSubmitButton();
	}

	@Override
	public void changeUserInfo(User user) {
		pages.internalPage.ensurePageLoaded()
			.clickUserProfilePage();
		pages.userProfilePage.ensurePageLoaded()
			.setUsernameField(user.getName())
			.setUserSurnameField(user.getSurname())
			.clickSaveButton()
			.ensureChangeUserInfo();		
	}

}
